import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xmshop/app/services/screebAdapter.dart';

import '../../../models/pcontent_model.dart';
import '../../../services/httpsClient.dart';
import '../../../services/cartServices.dart';

class ProductContentController extends GetxController {
  HttpsClient httpsClient = HttpsClient();
  // ScrollController，滚动控制器
  final ScrollController scrollController = ScrollController();

  // 定义全局key，给内容做标记
  GlobalKey gk1 = GlobalKey();
  GlobalKey gk2 = GlobalKey();
  GlobalKey gk3 = GlobalKey();

  // 容器位置
  double gk2Position = 0;
  double gk3Position = 0;

  // 是否显示详情tab
  RxBool showSubHeaderTabs = false.obs;
  // 商品详情tabs
  List subTabsList = [
    {"id": 1, "title": "商品介绍"},
    {"id": 2, "title": "规格参数"},
  ];
  RxInt selectSubTabsIndex = 1.obs;

  // 保存筛选属性值
  RxString selectedAttr = "".obs;

  // 商品详情数据
  var pcontent = PcontentItemtModel().obs;
  // 购买商品的数量
  RxInt count = 0.obs;

  // arrt
  RxList<PcontentAttrModel> pcontentAttr = <PcontentAttrModel>[].obs;

  List tabsList = [
    {
      "id": 1,
      "title": "商品",
    },
    {"id": 2, "title": "详情"},
    {"id": 3, "title": "推荐"}
  ];
  RxInt selectedTabsIndex = 1.obs;

  RxDouble opcity = 0.0.obs;
  RxBool showTabs = false.obs;
  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    scrollControlleraddListener();
  }

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
  }

  // 板顶滚动事件
  void scrollControlleraddListener() {
    scrollController.addListener(() {
      if (gk2Position == 0 && gk3Position == 0) {
        getContainerPosition(scrollController.position.pixels);
      }

      // 显示隐藏详情tab切换
      if (scrollController.position.pixels > gk2Position &&
          scrollController.position.pixels < gk3Position) {
        if (!showSubHeaderTabs.value) {
          showSubHeaderTabs.value = true;
          selectedTabsIndex.value = 2;
          update();
        }
      } else if (scrollController.position.pixels > 0 &&
          scrollController.position.pixels < gk2Position) {
        if (showSubHeaderTabs.value) {
          showSubHeaderTabs.value = false;
          selectedTabsIndex.value = 1;
          update();
        }
      } else if (scrollController.position.pixels > gk2Position) {
        if (showSubHeaderTabs.value) {
          showSubHeaderTabs.value = false;
          selectedTabsIndex.value = 3;
          update();
        }
      }

      // 显示隐藏顶部tab切换
      if (scrollController.position.pixels <= 100) {
        opcity.value = scrollController.position.pixels / 100;
        if (opcity.value > 0.9) {
          opcity.value = 1;
        }
        if (showTabs.value == true) {
          showTabs.value = true;
        }
        update();
      } else {
        if (showTabs.value == false) {
          showTabs.value = true;
          update();
        }
      }
    });
  }

  void changeSelectTabsIndex(index) {
    selectedTabsIndex.value = index;
    update();
  }

  // 获取商品详情数据
  void getContentData() async {
    var res = await httpsClient.get('/api/pcontent?id=${Get.arguments["id"]}');
    if (res != null) {
      var tempData = PcontentModel.fromJson(res.data);
      pcontent.value = tempData.result;
      pcontentAttr.value = pcontent.value.attr!;
      initAttr(pcontentAttr);
      getSelectedAttr();
      update();
    }
  }

  initAttr(RxList<PcontentAttrModel> attr) {
    for (var i = 0; i < attr.length; i++) {
      for (var j = 0; j < attr[i].list!.length; j++) {
        if (j == 0) {
          attr[i].attrList!.add({"title": attr[i].list![j], "checked": true});
        } else {
          attr[i].attrList!.add({"title": attr[i].list![j], "checked": false});
        }
      }
    }
  }

  // 改变选中状态
  changeAttr(cate, title) {
    for (var i = 0; i < pcontentAttr.length; i++) {
      if (pcontentAttr[i].cate == cate) {
        for (var j = 0; j < pcontentAttr[i].attrList!.length; j++) {
          pcontentAttr[i].attrList![j]['checked'] = false;
          if (pcontentAttr[i].attrList![j]['title'] == title) {
            pcontentAttr[i].attrList![j]['checked'] = true;
          }
        }
      }
    }
    update();
  }

  // 获取attr属性
  void getSelectedAttr() {
    List tempList = [];
    for (var i = 0; i < pcontentAttr.length; i++) {
      for (var j = 0; j < pcontentAttr[i].attrList!.length; j++) {
        if (pcontentAttr[i].attrList![j]['checked']) {
          tempList.add(pcontentAttr[i].attrList![j]["title"]);
        }
      }
    }
    selectedAttr.value = tempList.join(',');
  }

  // 获取元素的位置
  getContainerPosition(pixels) {
    // 第二屏
    RenderBox box2 = gk2.currentContext!.findRenderObject() as RenderBox;
    // 纵轴的位置数据
    gk2Position = box2.localToGlobal(Offset.zero).dy +
        pixels -
        (ScreenAdapter.getStatusBarHeight() + ScreenAdapter.height(120));

    // 第三屏
    RenderBox box3 = gk3.currentContext!.findRenderObject() as RenderBox;
    // 纵轴的位置数据
    gk3Position = box3.localToGlobal(Offset.zero).dy +
        pixels -
        (ScreenAdapter.getStatusBarHeight() + ScreenAdapter.height(120));
  }

  // 改变详情tab切换
  void changeSelectSunTabsIndex(index) {
    selectSubTabsIndex.value = index;
    // 跳转到指定位置
    scrollController.jumpTo(gk2Position);
    update();
  }

  // 增加商品数量
  icenCount() {
    count.value++;
    update();
  }

  // 减少商品数量
  decCount() {
    if (count.value > 1) {
      count.value--;
      update();
    }
  }

  // 加入购物车
  void addCart() {
    getSelectedAttr();
    CartServices.addCart(pcontent.value, selectedAttr.value, count.value);
    Get.snackbar('提示', '加入购物车成功');
    Get.back();
  }

  // 立即购买
  void buy() {
    getSelectedAttr();
    Get.back();
  }
}
